package com.awspaas.user.apps.shhtaerospaceindustrial.visiting;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.bpmn.engine.core.delegate.ProcessExecutionContext;
import com.actionsoft.bpms.bpmn.engine.listener.ExecuteListener;
import com.actionsoft.bpms.bpmn.engine.model.run.delegate.ProcessInstance;
import com.actionsoft.bpms.commons.database.ColumnMapRowMapper;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.actionsoft.sdk.local.api.BOAPI;
import com.actionsoft.sdk.local.api.ProcessAPI;
import com.awspaas.user.apps.shhtaerospaceindustrial.sms.SmsUtil;
import com.awspaas.user.apps.shhtaerospaceindustrial.util.CoreUtil;

public class VisitFeedback extends ExecuteListener {
	
	 @Override
	public String getDescription() {
	        return "参观申请流程结束，发起参观申请反馈和给申请人发短信提醒！";
	    }

	@SuppressWarnings("unchecked")
	@Override
	public void execute(ProcessExecutionContext pec) throws Exception {
		ProcessInstance processInstance = pec.getProcessInstance();
		String bindId = processInstance.getId();// 流程实例ID
		String headSql = "select * from BO_EU_INNERLOCATION_VISIT_APP where bindid='" + bindId + "' ";
		List<Map<String, Object>> headList = DBSql.query(headSql, new ColumnMapRowMapper(), new Object[] {});
		if (headList == null || headList.isEmpty()) {
			return;
		}
		BO headdata =new BO();
		Map<String, Object> dataMap = headList.get(0);
		String targetunit = CoreUtil.objToStr(dataMap.get("TARGETUNIT"));// 申请单位
		String targetman = CoreUtil.objToStr(dataMap.get("TARGETMAN"));// 申请人
		String visitornum = CoreUtil.objToStr(dataMap.get("VISITORNUM"));// 参观总人数
		String targetmanphone = CoreUtil.objToStr(dataMap.get("TARGETMANPHONE"));// 联系电话
		String ptryzzandname = CoreUtil.objToStr(dataMap.get("PTRYZZANDNAME"));// 陪同人员最高职务及姓名
		String id = CoreUtil.objToStr(dataMap.get("ID"));// ID
		headdata.set("TARGETUNIT", targetunit);//申请单位
		headdata.set("TARGETMAN", targetman);//申请人
		headdata.set("VISITORNUM", visitornum);//参观总人数
		headdata.set("TARGETMANPHONE", targetmanphone);//联系电话
		headdata.set("PTRYZZANDNAME", ptryzzandname);//陪同人员最高职务及姓名
		headdata.set("FID", id);//FID
		String[] type =visitUnit( bindId); 
		if(type.length >0) {
			//申请审批完成给申请人发短信
			sendmessage(type,targetmanphone);
			//发起参观申请反馈
			for (int i = 0; i < type.length; i++) {
				String officemanager = CoreUtil.objToStr(DBSql.getString(
						"select OFFICEMANAGER from BO_EU_VISIT_PERSON  where  TARGETUNIT = '" 
				+ type[i] + "'", "OFFICEMANAGER"));// 管理人员账号
				ArrayList bodydata =bodylist(bindId,type[i]);
				String kp_prosid = "obj_fc40c3614fc04cd2a6e52ef67395bc07";
				ProcessAPI ydproapi = SDK.getProcessAPI();
				BOAPI ydboapi = SDK.getBOAPI();
				ProcessInstance ydprocessinstance = 
						ydproapi.createProcessInstance(kp_prosid,officemanager, "--内部参观情况反馈");
				ydprocessinstance.getEndActivityId();
				ydboapi.create("BO_EU_VISIT_FEEDBACK", headdata, 
						ydprocessinstance, UserContext.fromUID(officemanager));	
				ydboapi.create("BO_EU_VISIT_ADDRESS", bodydata, ydprocessinstance,  UserContext.fromUID(officemanager));
				ydproapi.start(ydprocessinstance);
			}
			
		}		
		
	}
	//流程发起子表信息
public ArrayList  bodylist(String bindId,String type) {
	ArrayList body = new ArrayList();
	String bodySql = "select * from BO_EU_INNLOC_VISIT_APP_LOCS where bindid='" + bindId + "' ";
	List<Map<String, Object>> bodyList = DBSql.query(bodySql, new ColumnMapRowMapper(), new Object[] {});
	if (bodyList != null || bodyList.size() > 0) {
		for (int i = 0; i < bodyList.size(); i++) {
			BO boSonRecord = new BO();
			Map<String, Object> bodydata = bodyList.get(i);
			String TARGETUNIT = CoreUtil.objToStr(bodydata.get("TARGETUNIT"));// 参观单位
			String LOCATIONNAME = CoreUtil.objToStr(bodydata.get("LOCATIONNAME"));// 地点名称
			String VISITDATE = CoreUtil.objToStr(bodydata.get("VISITDATE"));// 预计参观日期
			String VISITDATETIME = CoreUtil.objToStr(bodydata.get("VISITDATETIME"));// 参观开始时间
			String VISITDATETIMEEND = CoreUtil.objToStr(bodydata.get("VISITDATETIMEEND"));// 参观结束时间
			boSonRecord.set("TARGETUNIT", TARGETUNIT);
			boSonRecord.set("LOCATIONNAME", LOCATIONNAME);
			boSonRecord.set("VISITDATE", VISITDATE);
			boSonRecord.set("VISITDATETIME", VISITDATETIME);
			boSonRecord.set("VISITDATETIMEEND", VISITDATETIMEEND);
			body.add(boSonRecord);
		}
	}
	return body;
  }

//可参观单位
public String[]  visitUnit(String bindId) {
	StringBuffer num = new StringBuffer("");
	String bodySql = "select * from BO_EU_INNLOC_VISIT_APP_LOCS where bindid='" + bindId + "' ";
	List<Map<String, Object>> bodyList = DBSql.query(bodySql, new ColumnMapRowMapper(), new Object[] {});
	if (bodyList != null || bodyList.size() > 0) {
		for (int i = 0; i < bodyList.size(); i++) {
			Map<String, Object> bodydata = bodyList.get(i);
			String TARGETUNIT = CoreUtil.objToStr(bodydata.get("TARGETUNIT"));// 参观单位
			if (i < bodyList.size() - 1) {
				num.append(TARGETUNIT);
				num.append("、");
			} else {
				num.append(TARGETUNIT);
				num.append("");
			}
		}
	}
	String[] unitlist= null;
	if(!"".equals(num.toString())){
		String[] visitunit = num.toString().split("、");
		ArrayList arrayList = new ArrayList();
		for (int i = 0; i < visitunit.length; i++) {
			String array = visitunit[i];
			if (!arrayList.contains(array)) {
				arrayList.add(array);
				}
			}
		unitlist = (String[]) arrayList.toArray(new String[arrayList.size()]);
	}
	return unitlist;
}
    //发送给发起人参观申请已通过审批
	public static void sendmessage(String[] type,String targetmanphone) throws java.lang.Exception {
		StringBuffer num = new StringBuffer("");
		for (int i = 0; i < type.length; i++) {
			String NAME = CoreUtil.objToStr(DBSql.getString(
					"select NAME from BO_EU_VISIT_PERSON  where  TARGETUNIT = '" 
			+ type[i] + "'", "NAME"));// 姓名
			String CELLPHONE = CoreUtil.objToStr(DBSql.getString(
					"select CELLPHONE from BO_EU_VISIT_PERSON  where  TARGETUNIT = '" 
			+ type[i] + "'", "CELLPHONE"));// 联系人电话
			if (i < type.length - 1) {
				num.append(type[i]+NAME+CELLPHONE);
				num.append("、");
			} else {
				num.append(type[i]+NAME+CELLPHONE);
				num.append("");
			}
		}
		String[] visitunit = num.toString().split("、");
		if(visitunit.length ==1) {
			String param = "{'ADDRESSONE':'" + visitunit[0] + "'}";
			SmsUtil.sendSms(targetmanphone, "SMS_463320100", param);
		}else if(visitunit.length ==2) {
			String param = "{'ADDRESSONE':'" + visitunit[0] +"','ADDRESSTWO':'"
					+ visitunit[1] + "'}";
			SmsUtil.sendSms(targetmanphone, "SMS_463510013", param);
		}else if(visitunit.length ==3) {
			String param = "{'ADDRESSONE':'" + visitunit[0] +"','ADDRESSTWO':'"
					+ visitunit[1] +"','ADDRESSTHREE':'" + visitunit[2]+ "'}";
			SmsUtil.sendSms(targetmanphone, "SMS_463360086", param);
		}else if(visitunit.length ==4) {
			String param = "{'ADDRESSONE':'" + visitunit[0] +"','ADDRESSTWO':'"
					+ visitunit[1] +"','ADDRESSTHREE':'" + visitunit[2]+
					"','ADDRESSFOUR':'" + visitunit[3]+"'}";
			SmsUtil.sendSms(targetmanphone, "SMS_463490013", param);
		}
	}

}
